﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OWNLib;

namespace OWNMonitor
{
    public partial class ProjectForm : Form
    {
        public OWNRefData RefData { get; private set; }
        public OWNProject Project { get; private set; }
        public string ProjectFileName { get; private set; }
        
        public ProjectForm(OWNRefData refData, OWNProject projectData, string projectFileName)
        {
            InitializeComponent();

            RefData = refData;
            Project = projectData;
            ProjectFileName = projectFileName;
        }

        private void ProjectForm_Load(object sender, EventArgs e)
        {
            whosBindingSource.DataSource = RefData.Whos; 
            oWNProjectBindingSourceWheres.DataSource = Project.Wheres;
            oWNProjectBindingSource.DataSource = Project;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBoxName.Text))
            {
                MessageBox.Show("Name field cannot be empty");
                this.DialogResult = DialogResult.None;   // The dialog shouldn't be closed...
                return; 
            }

            //Project.Name = TextBoxName.Text;
            Project.SaveXml(ProjectFileName);
        }

        private void buttonLoadCsv_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            if(fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Where[] wheres = OWNProject.LoadFromTxt(fileDialog.FileName, ",");
                    Project.Wheres.Clear();
                    foreach (Where where in wheres)
                    {
                        Project.Wheres.Add(where);
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }
    }
}
